﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Espam.Idiomas.Decimo.Tesis.Negocio;
using System.Text;
using System.IO;
using Microsoft.Reporting.WebForms;

namespace Espam.Idiomas.Decimo.Tesis.Web
{
    public partial class UI_ConsultScores : System.Web.UI.Page
    {
        string validaSession = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {


                Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
                Response.Cache.SetAllowResponseInBrowserHistory(false);
                Response.Cache.SetNoStore();

                if (Session["userEspamIdiomasVarSession001"] == null || Session["rolEspamIdiomasVarSession003"] == null)
                {
                    validaSession = "NoHaIniciadoSession";
                    Response.Redirect("UI_Login.aspx?validaSession=" + validaSession);
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Docente")
                {
                    Response.Redirect("UI_DashboardTeacher.aspx");
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Administradores")
                {
                    Response.Redirect("Default.aspx");
                }
                else if (Session["userEspamIdiomasVarSession001"] != null && Session["rolEspamIdiomasVarSession003"].ToString() == "Alumno")
                {
                    
                    lblvalida.Visible = false;
                    CargarGV();

                }
            }
        }

        protected void CargarGV()
        {
            Usuarios oUsuarios = new Usuarios();
            List<Usuarios> lista = new List<Usuarios>();
            oUsuarios.idAlumno = (int)Session["idAlumnoEspamIdiomasVarSession005"];
            lista = oUsuarios.GetCalificacionesByAlumno();
            if (lista.Count>0)
            {
                gvCalificaciones.DataSource = lista;
                gvCalificaciones.DataBind();
                rowAceptar.Visible = true;
                lblMensajeErrornotas.Text="NO DISPONIBLE";
                idMensajeNotas.Visible = false;
            }
            else
            {
                rowAceptar.Visible = false;
                lblMensajeErrornotas.Text = "NO DISPONIBLE";
                idMensajeNotas.Visible = true;
            }
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("UI_DashboardStudents.aspx");
        }


        protected void btnImprimirCalificaciones_Click(object sender, EventArgs e)
        {
            try
            {
                int idAlumnos = (int)Session["idAlumnoEspamIdiomasVarSession005"];
                EspamIdiomasDataSet.sp_getCalificacionesDataTable dt = new EspamIdiomasDataSet.sp_getCalificacionesDataTable();
                EspamIdiomasDataSetTableAdapters.sp_getCalificacionesTableAdapter da = new EspamIdiomasDataSetTableAdapters.sp_getCalificacionesTableAdapter();
                da.Fill(dt, idAlumnos);

                ReportViewer viewer = new ReportViewer();
                ReportDataSource RD = new ReportDataSource();
                RD.Value = dt;
                RD.Name = "DataSet1";
                //viewer.LocalReport.ReportPath = Server.MapPath("Reports/Teachers/AlumnosLListado.rdlc");
                viewer.LocalReport.DataSources.Clear();
                viewer.ProcessingMode = ProcessingMode.Local;
                viewer.LocalReport.DataSources.Add(RD);
                //viewer.LocalReport.ReportEmbeddedResource = "ComprobanteCalificaciones.rdlc";
                viewer.LocalReport.ReportPath = Server.MapPath("Reports/Students/ComprobanteCalificaciones.rdlc");
                viewer.LocalReport.Refresh();
                

                string mimeType;
                string encoding;
                string fileNameExtension;
                string[] streams;
                Warning[] warnings;
                byte[] pdfContent = viewer.LocalReport.Render("PDF", string.Empty, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

                this.Response.Clear();
                this.Response.ContentType = "application/pdf";
                this.Response.AddHeader("Content-disposition", "attachment; filename=ComprobanteCalificaciones.pdf");
                this.Response.BinaryWrite(pdfContent);
                this.Response.End();
            }
            catch (Exception)
            {

                throw;
            }

        }

    }
}